﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//■-----------------------------------■
//-----        卡组管理类          -----
//■-----------------------------------■
public class DeckManager : MonoBehaviour
{
    public CardDeck playerDeck;
    void Start()
    {
        string deckCardJson = OperateJson.GetJsonByPath("/Vash/Projects/Yogiho/Config/userDeckCfg.json");
        if (deckCardJson.Length > 0)
        {
            playerDeck = JsonUtility.FromJson<CardDeck>(deckCardJson);
        }
        else
        {
            Debug.LogWarning("json is empty");
        }
    }

    void Update()
    {

    }

    public void DeleteTopCardFormDeck()
    {
        CardBase temp = GetTopCardFormDeck();
        playerDeck.cards.Remove(temp);
    }

    public CardBase GetTopCardFormDeck()
    {
        return playerDeck.cards[0];
    }


    /// <summary>
    /// 洗牌算法
    /// </summary>
    public void Shuffle(int totalCount)
    {
        if (totalCount < 1)
        {
            return;
        }
        int randIndex = Random.Range(0, totalCount - 1);
        changeTwoCardPosInDeck(randIndex, totalCount - 1);
        Shuffle(totalCount - 1);
    }

    /// <summary>
    /// 交换卡组两个卡牌的位置
    /// </summary>
    /// <param name="indexa">交换值1卡组索引位置</param>
    /// <param name="indexb">交换值2卡组索引位置</param>
    public void changeTwoCardPosInDeck(int indexa, int indexb)
    {
        if (indexa >= 0 && indexb >= 0 &&
        indexa < playerDeck.cards.Count && indexb < playerDeck.cards.Count)
        {
            CardBase temp = playerDeck.cards[indexb];
            playerDeck.cards[indexb] = playerDeck.cards[indexa];
            playerDeck.cards[indexa] = temp;
        }
        else
        {
            Debug.LogWarning("wrong index");
        }
    }


    /// <summary>
    /// log出卡组目前的牌 从上往下的顺序
    /// </summary>
    public void printPlayerDeck()
    {
        string deckAttr = string.Format("卡组名字是{0},共有{1}张卡，分别是：\n", playerDeck.name, playerDeck.cards.Count);
        string cardsAttr = "";
        for (int i = 0; i < playerDeck.cards.Count; i++)
        {
            cardsAttr = cardsAttr + (i + 1) + ". " + playerDeck.cards[i].name + "\n";
        }
        Debug.Log(deckAttr + cardsAttr);
    }


}
